package com.yunxin.admin.system.api.system;


import com.yunxin.admin.system.application.RoleQueryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunxin.admin.system.application.RoleApplicationService;
import com.yunxin.admin.system.application.RoleQueryService;
import com.yunxin.admin.system.application.command.AddRoleCommand;
import com.yunxin.admin.system.application.command.RolePageCommand;
import com.yunxin.admin.system.application.command.UpdateRoleCommand;
import com.yunxin.admin.system.application.dto.OptionDTO;
import com.yunxin.admin.system.application.dto.RoleInfoDTO;
import com.yunxin.admin.system.application.dto.RolePageDTO;
import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
import com.yunxin.admin.system.infra.framework.resubmit.Resubmit;
import com.yunxin.admin.system.infra.framework.syslog.AutoLog;
import com.yunxin.common.result.PageResult;
import com.yunxin.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zero°
 * @since 2023-05-22
 */
@Tag(name = "03.角色接口")
@RestController
@RequestMapping("/api/v1/roles")
@RequiredArgsConstructor
public class SysRoleController {

    private final RoleApplicationService roleService;

    private final RoleQueryService roleQueryService;

    @Operation(summary = "角色分页列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/page")
    public PageResult<RolePageDTO> getRolePage(@ParameterObject RolePageCommand command) {
        Page<RolePageDTO> pageResult = roleQueryService.getRolePage(command);
        return PageResult.success(pageResult.getRecords(), pageResult.getTotal());
    }
//
    @Operation(summary = "角色下拉列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/options")
    public Result<List<OptionDTO<Long>>> listRoleOptions() {
        List<OptionDTO<Long>> list = roleQueryService.listRole();
        return Result.success(list);
    }

//    @Operation(summary = "新增角色", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
//    @PostMapping
//    @PreAuthorize("@ss.hasPerm('sys:role:add')")
//    @Resubmit
//    @AutoLog(value = "新增角色")
//    public Result<Boolean> addRole(@Valid @RequestBody AddRoleCommand command) {
//        boolean result = roleService.saveRole(command);
//        return Result.judge(result);
//    }
//
//    @Operation(summary = "角色表单数据", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
//    @GetMapping("/{roleId}/form")
//    public Result<RoleInfoDTO> getRoleForm(@Parameter(description = "角色ID") @PathVariable Long roleId) {
//        RoleInfoDTO roleInfoDTO = roleQueryService.getRoleById(roleId);
//        return Result.success(roleInfoDTO);
//    }
//
//    @Operation(summary = "修改角色", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
//    @PutMapping(value = "/{id}")
//    @PreAuthorize("@ss.hasPerm('sys:role:edit')")
//    @AutoLog(value = "修改角色")
//    public Result<Boolean> updateRole(@Valid @RequestBody UpdateRoleCommand command) {
//        boolean result = roleService.updateRole(command);
//        return Result.judge(result);
//    }
//
//    @Operation(summary = "删除角色", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
//    @DeleteMapping("/{ids}")
//    @PreAuthorize("@ss.hasPerm('sys:role:delete')")
//    @AutoLog(value = "删除角色")
//    public Result<Boolean> deleteRoles(@Parameter(description = "删除角色，多个以英文逗号(,)分割") @PathVariable String ids) {
//        boolean result = roleService.deleteRoles(ids);
//        return Result.judge(result);
//    }
//
//    @Operation(summary = "修改角色状态", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
//    @PutMapping(value = "/{roleId}/status")
//    @AutoLog(value = "修改角色状态")
//    public Result<Boolean> updateRoleStatus(@Parameter(description = "角色ID") @PathVariable Long roleId,
//            @Parameter(description = "状态(1:启用;0:禁用)") @RequestParam Integer status) {
//        boolean result = roleService.updateRoleStatus(roleId, status);
//        return Result.judge(result);
//    }
//
//    @Operation(summary = "获取角色的菜单ID集合", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
//    @GetMapping("/{roleId}/menuIds")
//    public Result<List<Long>> getRoleMenuIds(@Parameter(description = "角色ID") @PathVariable Long roleId) {
//        List<Long> menuIds = roleQueryService.getRoleMenuIds(roleId);
//        return Result.success(menuIds);
//    }
//
//    @Operation(summary = "分配菜单权限给角色", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
//    @PutMapping("/{roleId}/menus")
//    @AutoLog(value = "分配菜单权限给角色")
//    public Result<Boolean> updateRoleMenus(@PathVariable Long roleId,@RequestBody List<Long> menuIds) {
//        boolean result = roleService.updateRoleMenus(roleId, menuIds);
//        return Result.judge(result);
//    }
}
